package com.kuyun.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.entity.User1;
import com.kuyun.entity.UserComment;
import com.kuyun.service.LoginService;


@Controller
@RequestMapping(value = "/")
public class LoginController {
	@Resource
	private LoginService loginService;
	
	    //登录
		@RequestMapping(value = "dologin.json",method = RequestMethod.POST)
	    @ResponseBody
	    public Object doLogin(String userId,String password){
	        System.out.println("====================userId"+userId);
	        User1 user=loginService.getUser1(userId,null);
	        if (user!=null){
	            if (user.getPassword().equals(password)){
	                return user.getUserId();
	            }else{
	                return 0;
	            }
	        }else{
	            return -1;
	        }
	    }
		
		        //用户注册
				@RequestMapping(value = "doregister.json",method = RequestMethod.POST)
			    @ResponseBody
			    public Object doRegister(String userId,String userName,String password,int userAge,String userSex){
			        System.out.println("====================userId"+userId);
			        int register=loginService.doRegister(userId, userName, password, userAge, userSex);
			        if (register>0){
			            return 1;
			        }else{
			            return -1;
			        }
			    }

				//查询用户名
				@RequestMapping(value = "getUserName",method = RequestMethod.POST)
			    @ResponseBody
			    public Object getUserName(String userId, ModelMap map){
			        System.out.println("====================userId"+userId);
//			        String user=loginService.getUserName(userId);getUser1
			        Object user=loginService.getUser1(userId,null);
			        System.out.println("用户："+user);
//			        System.out.println("用户名："+user.getUserName());
			        if (user!=null){
			            return user;
			        }else{
			            return user;
			        }
			    }
				
				//保存记录信息
				@RequestMapping(value = "commentSave",method = RequestMethod.POST)
			    @ResponseBody
			    public int doCommentSave(String userId,String comment){
			        System.out.println("====================userId"+userId);
			        //获取当前时间
			        Date curDate = new Date(); //获取当前时间
//			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//			        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
			        User1 user=loginService.getUser1(userId,null);
			        String username = user.getUserName();
			        int resours=loginService.doCommentSave(userId, username, comment,curDate);
			        if (resours>0){
			            return 1;
			        }else{
			            return -1;
			        }
			    }
				
				
				//查询发表信息列表
				@RequestMapping(value = "getCommentList",method = RequestMethod.POST)
			    @ResponseBody
			    public Object getCommentList(String userId){
			        System.out.println("====================userId"+userId);
			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
			        List<UserComment> commentList=loginService.getCommentList(userId);
			        for(UserComment list:commentList){
			        	System.out.println(list.toString());
			        	System.out.println("====================userId"+formatter.format(list.getCurDate()));
			        }
			        return commentList;
			    }
				
				//删除发表信息
				@RequestMapping(value = "delectComment",method = RequestMethod.POST)
			    @ResponseBody
			    public int delectComment(String comment){
			        System.out.println("====================comment"+comment);
			        int re = loginService.delectComment(comment);
			        if(re > 0){
			        	return re;
			        }else {
			        	return -1;
					}
			    }
				
}
